package com.crunch.physics;

import com.crunch.math.Vector2f;

/**
 * An immutable segment.
 */
public class Segment {
	// constructs segment with points a, b; normal is vector (b-a) rotated 90 degrees CW
	public Segment(Vector2f a, Vector2f b) {
		if (a.equals(b)) {
			throw new IllegalArgumentException("Points are equal");
		}

		vertices = new Vector2f[] { new Vector2f(a), new Vector2f(b) };
		normal = b.subtract(a).rotateCwAssign().normalizeAssign();
	}

	public Vector2f getA() {
		return new Vector2f(vertices[0]);
	}

	public Vector2f getB() {
		return new Vector2f(vertices[1]);
	}

	public Vector2f getNormal() {
		return new Vector2f(normal);
	}

	// access without copying - for internal use only

	Vector2f[] getVerticesDirect() {
		return vertices;
	}

	Vector2f getNormalDirect() {
		return normal;
	}

	private Vector2f[] vertices;
	private Vector2f normal;
}
